/**
 * @author: zouye<yzyngz@foxmail.com>
 * @date: 2022-01-04 23:01:39
 * @copyright: zouye<yzyngz@foxmail.com>
 */
package com.yeah.voice.recognition.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yeah.voice.recognition.common.Properties;
import com.yeah.voice.recognition.common.Result;
import com.yeah.voice.recognition.entity.TbMarkData;
import com.yeah.voice.recognition.entity.TbWavData;
import com.yeah.voice.recognition.service.VoiceService;

/**
 * 音频相关接口
 */
@RestController
@RequestMapping("voice/")
public class VoiceController {

	@Autowired
	private VoiceService voiceService;
	
	@Autowired
	private Properties properties;
	
	/**
	 * 同一个人多次标记，避免重复重现已经被他标记过的音频；不同的人标记，优先让他们标记被标记次数最少的音频
	 * @param peopleId
	 * @return
	 */
	@RequestMapping("getOneVoiceName")
//	public Result getOneVoiceName(Integer peopleId) {
	public Result getOneVoiceName(Integer peopleId, String nextPlay) {
		try {
			TbWavData tbWavData = null;
			// 如果指定了本次要播放的文件名称，则直接使用，否则，自动获取一条音频
			if (nextPlay != null && !nextPlay.trim().equals("")) {
				tbWavData = voiceService.getWavFileByName(nextPlay.trim());
				// 指定的音频不存在，则使用自动获取的音频
				if (tbWavData  == null) {
					tbWavData = voiceService.getMinMarkedCountWav(peopleId);
				}
			} else {
				tbWavData = voiceService.getMinMarkedCountWav(peopleId);
			}
//			TbWavData tbWavData = voiceService.getMinMarkedCountWav(peopleId);
			if (tbWavData == null || tbWavData.getName() == null || tbWavData.getName().equals("")) {
				if (voiceService.hasWavFile()) {
					return Result.fail(404, "所有音频都已标记完成，感谢参与");
				} else {
					return Result.fail(404, "请联系管理员录入音频数据");
				}
			}
			
			return Result.ok(tbWavData);
		} catch(Exception e) {
			e.printStackTrace();
			return Result.fail(500, e.getMessage());
		}
	}
	
	/**
	 * 根据音频名称获取音频流文件
	 * @param name
	 * @param response
	 */
	@RequestMapping("getVoice")
	public void getVoice(String name, HttpServletResponse response) throws Exception {
		response.setContentType("audio/wav");
		response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + name);
		
		voiceService.readFileByName(name, response.getOutputStream());
	}
	
	/**
	 * 根据图片名称获取背景图片
	 * @param name
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("getImg	")
	public void getImg(String name, HttpServletResponse response) throws Exception {
		response.setContentType("application/octet-stream");
		response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + name);
		
		voiceService.readFileByName(name, response.getOutputStream());
	}
	
	/**
	 * 获取音频文件列表
	 * @return
	 */
	@RequestMapping("getVoiceList")
	public Result getVoiceList() {
		List<String> voiceList = voiceService.getFileList(properties.getVoiceLocation());
		return Result.ok(voiceList);
	}
}
